
/**
 *
 *
 *      opendu  Copyright (C) 2011  YouniS Ben Salah
 *
 *
 *      This file is part of opendu.
 *
 *      opendu is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      opendu is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with opendu.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *
 *      Program                 ::      opendu
 *      Version                 ::      1.02, beta
 *      Date                    ::      July, August, September, October, November, 2011
 *
 *      Author                  ::      YouniS Ben Salah
 *      E-Mail                  ::      YouniS.BenSalah02@yahoo.de
 *      Website                 ::      http://myhttp.funpic.de
 *
 *      Module                  ::      I/O
 *      C File                  ::      yio.c
 *      Revision                ::      {}
 *
 *      ToDo                    ::      + Write Documentation for draw*() functions !!
 *
 *
 */




 #include "yinclude.h"




/*
 *
 *
 *  Function                ::      drawScreen()
 *
 *  Draw the whole screen.
 *
 *
 *      [  opendu 1.02, beta  ]     <--- program name && version
 *
 *      [  _ _ L _ B  ]     <--- hidden word
 *
 *      [  (<3) (<3) (<3) (<3) (<3) (<3) (  ) (  ) (  ) (  )  ]     <--- number of lifes
 *
 *              _______
 *              |/    |         <--- hangman
 *              |     @
 *              |    -O-
 *              |     |\
 *              |
 *             /_\
 *
 *      [  [X] [Y] [C]  ]   <--- used letters
 *
 *      [  developed by :: YouniSHD  ]  <--- author
 *
 *
 */

void drawScreen( Game *Imagame, int cols, int n )
{
    int i;
    char *hidden_word = NULL;
    drawBorder( cols );
    drawCentered( VERSION, cols );
    drawBorder( cols );
    drawEmptyLine( cols );

    hidden_word = malloc( sizeof(char)*STR_SIZE );
    if ( hidden_word == NULL ) {
        exit(ERR_ALLOC); /* can't allocate memory ): */
    }
    for ( i=0; i<strlen(Imagame->Hidden_word); i++ ) {
        *(hidden_word+i) = *((Imagame->Hidden_word)+i);
        *(hidden_word+(2*i+1)) = ' ';
    }
    *(hidden_word+(2*i)) = '\0'; /* check this !!!!!!!!!!! */
    drawNFromLeft( hidden_word, cols, n );

    drawEmptyLine( cols );
    drawNFromLeft( drawLifes( Imagame ), cols, n );

    drawEmptyLine( cols );
    drawHangman( Imagame );

    drawEmptyLine( cols );
    drawNFromLeft( drawUsedLetters( Imagame ), cols, n );

    drawEmptyLine( cols );
    drawBorder( cols );
    drawCentered( DEVEL, cols );
    drawBorder( cols );
}



void drawCentered( char *text, int cols )
{
    int n, m, i;

    n = (cols-strlen(text))/2-1;
    m = ((cols-strlen(text))/2)+((cols-strlen(text))%2)-1;

    printf("|");
    for ( i=0; i<n; printf(" "), i++ );
    printf("%s", text);
    for ( i=0; i<m; printf(" "), i++ );
    printf("|\n");
}



void drawNFromLeft( const char *text, const int cols, const int n )
{
    int m, i;

    m = cols - n - strlen(text) - 2;

    printf("|");
    for ( i=0; i<n; printf(" "), i++ );

    if ( m < 0 ) {  /* if word too long */
        /* split word */
        for ( i=0; i<strlen(text)+m; printf("%c", *(text+(i++))) );
        printf("|\n");
        drawNFromLeft( text+i, cols, n );  /* recursing */
    }
    else {  /* word not too long */
        printf("%s", text);
        for ( i=0; i<m; printf(" "), i++ );
        printf("|\n");
    }
}



void drawBorder( int cols )
{
    int i;

    printf("+");
    for ( i=0; i<(cols-2); printf("-"), i++ );
    printf("+\n");
}



void drawEmptyLine( int cols )
{
    int i;

    printf("|");
    for ( i=0; i<(cols-2); printf(" "), i++ );
    printf("|\n");
}



char *drawLifes( Game *Imagame )
{
    char *result = NULL;
    result = malloc( sizeof(char)*STR_SIZE );
    if ( result == NULL ) {
        exit(ERR_ALLOC); /* can't allocate memory ): */
    }
    *result = '\0';

    sprintf( result, "Lifes ::    %d / %d", Imagame->Number_of_lifes, NUMBER_OF_LIFES );

    return result;
}



void drawHangman( Game *Imagame )
{
    switch ( Imagame->Number_of_lifes ) {
        case 0 :
        {
            drawNFromLeft(" _______", COLS, MARGIN*2);
            drawNFromLeft(" |/    |", COLS, MARGIN*2);
            drawNFromLeft(" |     @", COLS, MARGIN*2);
            drawNFromLeft(" |    -O-", COLS, MARGIN*2);
            drawNFromLeft(" |     |\\", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft("/_\\", COLS, MARGIN*2);
            break;
        }
        case 1 :
        {
            drawNFromLeft(" _______", COLS, MARGIN*2);
            drawNFromLeft(" |/    |", COLS, MARGIN*2);
            drawNFromLeft(" |     @", COLS, MARGIN*2);
            drawNFromLeft(" |    -O-", COLS, MARGIN*2);
            drawNFromLeft(" |      \\", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft("/_\\", COLS, MARGIN*2);
            break;
        }
        case 2 :
        {
            drawNFromLeft(" _______", COLS, MARGIN*2);
            drawNFromLeft(" |/    |", COLS, MARGIN*2);
            drawNFromLeft(" |     @", COLS, MARGIN*2);
            drawNFromLeft(" |    -O-", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft("/_\\", COLS, MARGIN*2);
            break;
        }
        case 3 :
        {
            drawNFromLeft(" _______", COLS, MARGIN*2);
            drawNFromLeft(" |/    |", COLS, MARGIN*2);
            drawNFromLeft(" |     @", COLS, MARGIN*2);
            drawNFromLeft(" |    -O", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft("/_\\", COLS, MARGIN*2);
            break;
        }
        case 4 :
        {
            drawNFromLeft(" _______", COLS, MARGIN*2);
            drawNFromLeft(" |/    |", COLS, MARGIN*2);
            drawNFromLeft(" |     @", COLS, MARGIN*2);
            drawNFromLeft(" |     O", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft("/_\\", COLS, MARGIN*2);
            break;
        }
        case 5 :
        {
            drawNFromLeft(" _______", COLS, MARGIN*2);
            drawNFromLeft(" |/    |", COLS, MARGIN*2);
            drawNFromLeft(" |     @", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft("/_\\", COLS, MARGIN*2);
            break;
        }
        case 6 :
        {
            drawNFromLeft(" _______", COLS, MARGIN*2);
            drawNFromLeft(" |/    |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft("/_\\", COLS, MARGIN*2);
            break;
        }
        case 7 :
        {
            drawNFromLeft(" _______", COLS, MARGIN*2);
            drawNFromLeft(" |/", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft("/_\\", COLS, MARGIN*2);
            break;
        }
        case 8 :
        {
            drawNFromLeft(" ", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft(" |", COLS, MARGIN*2);
            drawNFromLeft("/_\\", COLS, MARGIN*2);
            break;
        }
        case 9 :
        {
            drawNFromLeft(" ", COLS, MARGIN*2);
            drawNFromLeft(" ", COLS, MARGIN*2);
            drawNFromLeft(" ", COLS, MARGIN*2);
            drawNFromLeft(" ", COLS, MARGIN*2);
            drawNFromLeft(" ", COLS, MARGIN*2);
            drawNFromLeft(" ", COLS, MARGIN*2);
            drawNFromLeft("/_\\", COLS, MARGIN*2);
            break;
        }
        default :
        {
            drawNFromLeft(" .", COLS, MARGIN*2);
        }
    }
}



char *drawUsedLetters( Game *Imagame )
{
    /*
    char *result = NULL;
    result = malloc( sizeof(char)*STR_SIZE );
    if ( result == NULL ) {
        exit(ERR_ALLOC);
    }
    *result = '\0';
    */


    /* change me ! */
    return Imagame->List_of_used_letters;
}



void clearScreen()
{
    system("clear");    /* UNIX command */
}



/*
 *
 *
 *  Function                ::      write()
 *
 *  Write some outputs to screen like menu, credits etc ...
 *
 *
 */

void write( int stuff )
{
    if ( stuff      == MENU ) {
        printf("\n");
        drawBorder( COLS );
        drawEmptyLine( COLS );
        drawCentered("opendu 1.02 >>> developed by YouniSHD", COLS);
        drawEmptyLine( COLS );
        drawBorder( COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawNFromLeft( "[1 ][ play]", COLS, 2*MARGIN );
        drawNFromLeft( "[2 ][ credits]", COLS, 2*MARGIN );
        drawNFromLeft( "[3 ][ highscore]", COLS, 2*MARGIN );
        drawNFromLeft( "[4 ][ help]", COLS, 2*MARGIN );
        drawNFromLeft( "[5 ][ quit]", COLS, 2*MARGIN );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawBorder( COLS );
        printf("\n");
    }

    else if ( stuff == PROMPT ) {
        printf("\n>>> ");
    }

    else if ( stuff == CREDITS ) {
        drawBorder( COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );

        drawCentered( "opendu", COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawCentered( "version", COLS );
        drawCentered( "1.02, beta", COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawCentered( "developed by", COLS );
        drawCentered( "YouniS Ben Salah", COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawCentered( "YouniS.BenSalah02@yahoo.de", COLS );
        drawEmptyLine( COLS );
        drawCentered( "http://myhttp.funpic.de", COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawCentered( "opendu  Copyright (C) 2011  YouniS Ben Salah", COLS );
        drawCentered( "------", COLS );

        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawBorder( COLS );

        /* a return to menu hint ! */
        printf( "\n>>> type 0 to return to menu.");
    }

    else if ( stuff == HELP ) {
        printf("\n");

        drawBorder( COLS );
        drawEmptyLine( COLS );
        drawNFromLeft( "What is opendu ?", COLS, MARGIN );
        drawNFromLeft( "****************", COLS, MARGIN );
        drawEmptyLine( COLS );
        drawNFromLeft( "opendu is a simple Hangman game for Linux/Unix console. It is written in C.", COLS, 1 );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );

        drawNFromLeft( "How to play Hangman ?", COLS, MARGIN );
        drawNFromLeft( "*********", COLS, MARGIN );
        drawNFromLeft( "Hangman is a two-player-game.", COLS, 1 );
        drawNFromLeft( "Player 1 enters a word. The word will be hidden then.", COLS, 1 );
        drawNFromLeft( "Player 2 only sees dashes ( this is a dash - ) in place of each letter.", COLS, 1);
        drawNFromLeft( "Player 2 then tries to guess the word by typing some letters. Everytime he enters a letter,"
                       " the game will then indicate him if the word actually contains the typed letter.", COLS, 1);
        drawNFromLeft( "If it contains the letter, then the letter will be visible in the word. (If a letter exists"
                       " more than one time in the word, all those letters will be visible.)", COLS, 1);
        drawNFromLeft( "If it doesn't contain the entered letter, the player will lose a life.", COLS, 1);
        drawNFromLeft( "The game ends either when P2 guesses the word (P2 wins the game),"
                       " or when P2 has no remaining life. In that case, P1 wins the game.", COLS, 1);
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );

        drawNFromLeft( "Example!?", COLS, MARGIN );
        drawNFromLeft( "*********", COLS, MARGIN );
        drawNFromLeft( "Okay here's an example of how an opendu game could look like ::", COLS, 1);
        drawEmptyLine( COLS );
        drawNFromLeft( "P1 entered the following word >>> WINDOWS", COLS, 1);
        drawNFromLeft( "* The word will be hidden now *", COLS, 1);
        drawEmptyLine( COLS );
        drawNFromLeft( "P2 now sees this", COLS, 1);
        drawNFromLeft( "- - - - - - -", COLS, 1);
        drawEmptyLine( COLS );
        drawNFromLeft( "P2 tries to guess the word. He enters >>> A", COLS, 1);
        drawNFromLeft( "* A doesn't exist in the word, P2 loose a life.", COLS, 1);
        drawNFromLeft( "- - - - - - -", COLS, 1);
        drawEmptyLine( COLS );
        drawNFromLeft( "P2 tries I", COLS, 1);
        drawNFromLeft( "* I exists, so all the I's in the word become visible.", COLS, 1);
        drawNFromLeft( "- I - - - - -", COLS, 1);
        drawEmptyLine( COLS );
        drawNFromLeft( "P2 tries W", COLS, 1);
        drawNFromLeft( "* There are 2 W's in the word. *", COLS, 1);
        drawNFromLeft( "W I - - - W -", COLS, 1);
        drawEmptyLine( COLS );
        drawNFromLeft( "P2 enters S, D, O, and N ...", COLS, 1);
        drawNFromLeft( "W I N D O W S", COLS, 1);
        drawEmptyLine( COLS );
        drawNFromLeft( "P2 wins the game :D", COLS, 1);
        drawEmptyLine( COLS );
        drawBorder( COLS );

        /* a return to menu hint ! */
        printf( "\n>>> type 0 to return to menu.");
    }

    else if ( stuff == SPLASH ) {
        drawBorder( COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawCentered( " _______   _______   _______  ___    __   ______    __   __", COLS );
        drawCentered( "|  ___  | |  ____ | |   ___/ |   \\  |  | |      \\  |  | |  |", COLS );
        drawCentered( "| |   | | | |___/ | |  |___  |    \\ |  | |  ___  | |  | |  |", COLS );
        drawCentered( "| |   | | |  ____/  |   __/  |  |\\ \\|  | | |   | | |  | |  |", COLS );
        drawCentered( "| |__/  | | |       |  |____ |  | \\    | | |__/ /  |  |_|  |", COLS );
        drawCentered( "|______/  |_|       |______/ |__|  \\__/  |_____/   |______/", COLS );
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawCentered( "************************************", COLS);
        drawCentered( "* developed by :: YouniS Ben Salah *", COLS);
        drawCentered( "************************************", COLS);
        drawEmptyLine( COLS );
        drawEmptyLine( COLS );
        drawNFromLeft( "1.02, beta", COLS, COLS-13 );
        drawBorder( COLS );
    }

    else if ( stuff == HIGHSCORES ) {
        /* highscores not implemented yet .. i am lazy */
        printf("\nHIGHSCORES\n\n 1.  Chuck Norris\n"
               " 2.  Chuck Norris\n"
               " 3.  Chuck Norris\n"
               " 4.  Chuck Norris\n"
               " 5.  Chuck Norris\n"
               " 6.  Chuck Norris\n"
               " 7.  Chuck Norris\n"
               " 8.  Chuck Norris\n"
               " 9.  Chuck Norris\n"
               "10.  Chuck Norris\n\n");

        /* a return to menu hint ! */
        printf( "\n>>> type 0 to return to menu.");
    }

    else if ( stuff == QUIT ) {
        printf("\nokay, ciya (:\n\n\nopendu\n\nprogram written by YouniS Ben Salah\n\n\n");
    }

    else if ( stuff == BAD_CMD ) {
        printf("wtf? the menu item you entered is invalid.\n");
    }

    else {
        printf("::fixme! unknown stuff passed to write() function.\n");
    }
}



/*
 *
 *
 *  Function                ::      readCh()
 *
 *  Read character and return it.
 *
 *  Y U NO WORK !!??  O_o
 *
 *
 */

char readCh()
{
    char wtf = 0;
    wtf = getchar();
    while ( getchar() != '\n' );
    return wtf;
}









